﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SalaryChannel.TaskWebService;

namespace SalaryChannel
{
    public partial class MyJob : UserControl
    {
        // TaskServiceInstance.TaskServiceClient ServiceInstance.TaskService = ServiceInstance.TaskServiceInstance.TaskServiceInstance.TaskService;
        List<VW_TaskOn> myTask;
        public MyJob()
        {
            InitializeComponent();
            DateTime now = DateTime.Now;
            int Months = -1;
            while (Months > -12)
            {
                DateTime dt = now.AddMonths(Months);
                this.MonCBox.Items.Add(dt.Year * 100 + dt.Month);
                Months--;
            }

            MonCBox.SelectionChanged += new SelectionChangedEventHandler(MonCBox_SelectionChanged);
            ServiceInstance.TaskService.GetMyJobCompleted += new EventHandler<GetMyJobCompletedEventArgs>(TaskService_GetMyJobCompleted);
                                  
        }

        void MonCBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
                CurStepTB.Text = string.Empty;
                StartTB.Text = string.Empty;
                LeftHoursTB.Text = string.Empty;
              //  CurJobComBox.Items.Clear();
                LastUser.Text = string.Empty;
                if (orignal != null)
                {
                    orignal.Visibility = Visibility.Collapsed;
                }
            
            
            if (CurJobComBox.Items.Count != 0)
            {
                CurJobComBox.Items.Clear();
            }

            if (MonCBox.SelectedItem == null)
            {
                return;
            }
            try
            {
                ServiceInstance.TaskService.GetMyJobAsync(App.CurrentUser.UserID, int.Parse(MonCBox.SelectedItem.ToString()), Guid.NewGuid());
            }
            catch
            {
                App.MessageBox.ShowDialog("初始化失败");
            }
        }


        void TaskService_GetMyJobCompleted(object sender, GetMyJobCompletedEventArgs e)
        {
            try
            {
                if (e.Result == null)
                {
                    App.MessageBox.ShowDialog("查询当前任务失败，服务器异常");

                }
                else
                {
                    myTask = e.Result;
                    var query = from q in myTask
                                orderby q.Task_Month
                                select new { q.Task_ID, q.Task_Name };
                   
                    foreach (var task in query.Distinct())
                    {
                        CurJobComBox.Items.Add(string.Format("[{0}]{1}",task.Task_ID,task.Task_Name));
                    }
                    if (CurJobComBox.Items.Count > 0)
                    {
                        CurJobComBox.SelectedIndex = 0;
                    }
                }
            }
            catch (Exception ex)
            {

               App.MessageBox.ShowDialog("查询当前任务失败:"+ex.Message);
            }
        }

        public void Initiallize()
        {
            MonCBox.SelectedIndex = -1;
        }

        private Control orignal = null;

        /// <summary>
        /// 选择任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CurJobComBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CurStepTB.Text = string.Empty;
            StartTB.Text = string.Empty;
            LeftHoursTB.Text = string.Empty;
            if (orignal != null)
            {
                orignal.Visibility = Visibility.Collapsed;
            }
            if (CurJobComBox.SelectedItem == null)
            {
                return;
            }
            var query = from q in myTask
                        where q.Task_ID == App.GetID(CurJobComBox.SelectedItem.ToString())
                        orderby q.Start_Time
                        select q;
            VW_TaskOn  CurTaskOn = query.First();

            CurStepTB.Text = string.Format("[{0}]{1}", CurTaskOn.Step_ID, CurTaskOn.Step_Name);
            StartTB.Text = CurTaskOn.Start_Time.ToShortDateString() + " " + CurTaskOn.Start_Time.ToShortTimeString() ;
            LastUser.Text = CurTaskOn.LastUser;
            int leftHours=CurTaskOn.Left_Hours.Value;
            
            LeftHoursTB.Text = leftHours.ToString();
            if (leftHours < 10)
            {
                LeftHoursTB.Foreground = new SolidColorBrush(Colors.Red);
            }
            else
            {
                LeftHoursTB.Foreground = new SolidColorBrush(Colors.Black);
            }
            if (leftHours == 0)
            {
                App.MessageBox.ShowDialog("任务已超时，无法操作！您可以向管理员申请延期。", Title.Warning);
                return;
            }
            
            foreach (Control ctrl in JobCtrlContainer.Children)
            {
                if (ctrl.Tag.Equals(CurTaskOn.Ctrl_Name))
                {
                    ctrl.HorizontalAlignment = HorizontalAlignment.Stretch;
                    ctrl.VerticalAlignment = VerticalAlignment.Stretch;
                    ctrl.Visibility = Visibility.Visible;
                    (ctrl as IJobCtrl).Initiallize(CurTaskOn);
                    orignal = ctrl;
                    break;
                }
            }
        }

        
    }
}
